<?php
    class IndexController extends Main_Controller_Action
    {   
        public function init()
        {
            $this->loadTemplates();
        }
        
        public function indexAction()
        {
            $this->view->headTitle('t\'house - Trang chủ');
            $fashion = new Default_Model_Fashion();
            $baby = new Default_Model_Baby();
            $furniture = new Default_Model_Furniture();
            $this->view->fashion = $fashion->getListItemForIndex();
            $this->view->baby = $baby->getListItemForIndex();
            $this->view->furniture = $furniture->getListItemForIndex();
        }
        
        public function searchAction()
        {
            $this->view->headTitle('t\'house - Tìm kiếm');
            $keyword = trim(strip_tags($this->_request->getParam('text-search')));
            if((strlen($keyword) < 2) || (strlen($keyword) > 50)) {
                $this->view->data = array();
            }
            else {
                $search = new Default_Model_Search();
                $this->view->keyword = $keyword;
                $keyword = (string) '%'.str_replace(' ','%',$keyword).'%';
                $fashion = $search->search($keyword, 'fashion');
                $baby = $search->search($keyword, 'baby');
                $furniture = $search->search($keyword, 'furniture');
                $data = array_merge($fashion, $baby, $furniture);
                $paginator = Zend_Paginator::factory($data);
                $paginator->setItemCountPerPage(30);
                $paginator->setPageRange(5);
                $currentPage = $this->_request->getParam('page', 1);
                $paginator->setCurrentPageNumber($currentPage);
                $this->view->data = $paginator;
                $this->view->total = count($data);
            }
        }
    }
